<?php

namespace App\Model;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Auth;

/**
 * App\Model\UserInterested
 *
 * @property int $id
 * @property int $user_id 0游客
 * @property int $target_user_id 目标用户
 * @property string $ip ip
 * @property string|null $name 姓名
 * @property string|null $phone 手机
 * @property \Illuminate\Support\Carbon|null $created_at
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\UserInterested newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\UserInterested newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\UserInterested query()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\UserInterested whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\UserInterested whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\UserInterested whereIp($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\UserInterested whereName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\UserInterested wherePhone($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\UserInterested whereTargetUserId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Model\UserInterested whereUserId($value)
 * @mixin \Eloquent
 */
class UserInterested extends Model
{
	protected $table = 'user_interested';

	/**
	 * @param $target 感兴趣用户id
	 * @param $name    名字
	 * @param $phone 电话
	 * @return bool
	 */
	public static function add($target, $name, $phone): bool
	{
		$user_id = Auth::check() ? Auth::id() : 0;
		$ip = \request()->getClientIp();
		if ($user_id == $target) {
			return true;
		}
		if ($user_id > 0) {
			$log = self::where('user_id', '=', $user_id)->where('target_user_id', '=', $target)->first();

			if (!$log) {
				return self::insert([
					'user_id' => $user_id,
					'target_user_id' => $target,
					'ip' => $ip,
					'name' => $name,
					'phone' => $phone,
					'created_at' => Carbon::now()
				]);
			}
		} else {
			$log = self::where('ip', '=', $ip)->where('user_id', 0)->where('target_user_id', '=', $target)->first();

			if (!$log) {
				return self::insert([
					'user_id' => $user_id,
					'target_user_id' => $target,
					'ip' => $ip,
					'name' => $name,
					'phone' => $phone,
					'created_at' => Carbon::now()
				]);
			}
		}
		return true;
	}

	/**
	 * 是否已经有记录
	 */
	public static function isInterested($target):bool
	{
		$user_id = Auth::check() ? Auth::id() : 0;
		$ip = \request()->getClientIp();
		if ($user_id == $target) {
			return true;
		}
		if ($user_id > 0) {
			$log = self::where('user_id', '=', $user_id)->where('target_user_id', '=', $target)->first();
			if ($log){
				return true;
			}
		}else{
			$log = self::where('ip', '=', $ip)->where('user_id', 0)->where('target_user_id', '=', $target)->first();
			if ($log){
				return true;
			}
		}
		return false;
	}
}
